package com.jic.market.impl.rest.discount.processor;

import com.jic.market.common.util.Money;
import com.jic.market.entity.PromotionRuleEntity;
import com.jic.market.enums.DiscountAmountServiceOperateEnum;
import com.jic.market.impl.rest.discount.model.ConditionDiscountModel;
import com.jic.market.impl.rest.discount.model.GoodsDetailModel;
import com.jic.market.mapper.PromotionRuleEntityMapper;
import com.jic.market.request.rest.discount.DiscountBaseRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 组合-X件付Y件
 * @author yinhaijun
 * @date: 2020/5/19
 */
@Service("ComplexXjianFuyProcessor")
public class ComplexXjianFuyProcessor extends AbstractConditionDiscountProcessor {

    @Autowired
    private PromotionRuleEntityMapper promotionRuleEntityMapper;
    @Override
    public ConditionResult isCondition(ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request, DiscountAmountServiceOperateEnum operate) {
        if(!checkPromotion(((DiscountBaseRequest)request).getThirdUserId(), ((DiscountBaseRequest)request).getAdminOperate().getChildId().toString(), conditionDiscountModel.getChannelCodes(), conditionDiscountModel.getPromotionMemberGradeList())){
            return new ConditionResult(false, "组合计算"+conditionDiscountModel.getPromotionCode()+"会员等级校验未通过");
        }
        GoodsDetailModel goodsDetailModel = goodsDetailModelMap.get(conditionDiscountModel.getThirdSkuIdList().get(0));
        List<PromotionRuleEntity> promotionRuleEntityList = promotionRuleEntityMapper.queryByPromotionId(Long.parseLong(conditionDiscountModel.getPromotionId()));
        if(null == promotionRuleEntityList || promotionRuleEntityList.isEmpty()){
            return new ConditionResult(false, "没有规则数据");
        }
        if(goodsDetailModel.getNum() < promotionRuleEntityList.get(0).getConditionValue()){//X
            return new ConditionResult(false, "未达到条件");
        }
        ConditionResult conditionResult = new ConditionResult(true, "通过");
        conditionResult.setObject(promotionRuleEntityList.get(0).getContentValue());//Y
        return conditionResult;
    }

    @Override
    public void queryShoppingCartAfter(ConditionResult conditionResult, ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request){
        querySettlementAfter(conditionResult,  conditionDiscountModel, goodsDetailModelMap, request);
    }

    @Override
    public void querySettlementAfter(ConditionResult conditionResult, ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request){
        // 付Y件
        BigDecimal y = (BigDecimal) conditionResult.getObject();
        // 优惠总价 = y * 原价
        BigDecimal promotionDiscountTotalAmount =  Money.multiply(y, goodsDetailModelMap.get(conditionDiscountModel.getThirdSkuIdList().get(0)).getSalePrice());
        conditionDiscountModel.setPromotionDiscountTotalAmount(promotionDiscountTotalAmount);
    }

}

